/**
 * 
 */
package com.miolr.module.core.service;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.miolr.module.core.config.HibernateDevConfig;
import com.miolr.module.core.config.SocialConfig;
import com.miolr.module.core.model.User;

/**
 * @author iday
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { HibernateDevConfig.class, SocialConfig.class })
@ActiveProfiles("dev")
public class UserServiceTestCase {
	@Autowired
	UserService service;
	User user;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		user = new User();
		user.setUsername("Ya-h0o_ya.hoo.com");
		user.setEmail("yahoo@yahoo.com");
		user.setPassword("test");
		user.addAuthority("ROLE_ADMIN");
		service.save(user);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		service.delete(user);
		user = null;
	}

	/**
	 * Test method for {@link com.miolr.module.core.service.UserService#updateUser(com.miolr.module.core.model.User)}.
	 */
	@Test
	
	public void testUpdateUser() {
		user.setPassword("test1");
		service.update(user);
		assertEquals("test1", user.getPassword());
	}

	/**
	 * Test method for {@link com.miolr.module.core.service.UserService#loadUserByUsername(java.lang.String)}.
	 */
	@Test
	public void testLoadUserByUsername() {
		User u = (User) service.loadUserByUsername("Ya-h0o_ya.hoo.com");
		assertEquals("ROLE_ADMIN", u.getAuthorities().iterator().next().getAuthority());
		assertEquals("Ya-h0o_ya.hoo.com", u.getUsername());
	}

}
